//
//  NewFeatureViewController.swift
//  SinaWeiBo
//
//  Created by dengchen on 15/11/12.
//  Copyright © 2015年 name. All rights reserved.
//

import UIKit

private let ImageCount = 4

class NewFeatureViewController: UIViewController,UIScrollViewDelegate {


    
    func scrollViewDidEndDecelerating(scrollView1: UIScrollView) {

    let page:Int = Int(scrollView1.contentOffset.x/DcScreenwidth) + 1
        
        if page == ImageCount {
            startAnimate()
        }
    }
    
    private func startAnimate() {
        
        //usingSpringWithDamping  弹簧系数  0~1之间  越小越弹
        //initialSpringVelocity 加速度
        //弹簧效果 和加速度之间的关系: 弹簧效果 * 10 = 加速度
        
        UIView.animateWithDuration(1, delay: 0.25, usingSpringWithDamping: 0.5, initialSpringVelocity: 8.8, options: [], animations: { () -> Void in
            
            self.btn.transform = CGAffineTransformMakeScale(1, 1)
            
            }) { (_) -> Void in
                
        }
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    private func setupUI() {
        
        scrollView.delegate = self
        
        view.addSubview(scrollView)
        scrollView.addSubview(btn)

        for i in 1...ImageCount {
           
        let imageView:UIImageView = UIImageView(frame: CGRect(x:CGFloat(i - 1)*DcScreenwidth, y: 0, width: DcScreenwidth, height: DcScreenheight))
            imageView.image = UIImage(named: "new_feature_\(i)")
            scrollView .addSubview(imageView)
        }
        
        let w:CGFloat = 200;
        let h:CGFloat = 50;
        let margin:CGFloat = (DcScreenwidth - w) * 0.5
        let x = CGFloat(ImageCount - 1) * DcScreenwidth + margin + 5.0
        let y = DcScreenheight - 220.0
        
        btn.frame = CGRect(x: x, y: y, width: w, height: h)
        btn.transform = CGAffineTransformMakeScale(0, 0)
        btn.addTarget(self, action: "btnClick", forControlEvents: .TouchUpInside)
    }
    
    private lazy var scrollView:UIScrollView = {
        
        let sc = UIScrollView(frame: DcScreenbounds)
        sc.bounces = false
        sc.showsHorizontalScrollIndicator = false
        sc.pagingEnabled = true
        sc.contentSize = CGSize(width:DcScreenwidth * CGFloat(ImageCount), height: 0)
        
        return sc
        }()
    
    
    private lazy var btn:UIButton =
    UIButton(title: "立即体验", bgImge: "new_feature_finish_button", titleColor: UIColor.whiteColor())
    
    @objc private func btnClick() {
        NSNotificationCenter.defaultCenter().postNotificationName(backToHome, object: nil)
    }

}


